﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Net;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.ChildWindows;
using FRL.DebtChased.Extensions;
using FRL.DebtChased.Silverlight.Library.RichContentService;
using FRL.DebtChased.Silverlight.Library;
using FRL.DebtChased.Views;

namespace FRL.DebtChased.ChaseWorkspace
{

    /// <summary>
    /// Declaration of MainPage class
    /// </summary>
    public partial class MainPage : UserControl, IDebtChasedMainPlugInWindow
    {

        /////////////////////////////////////////////////////////////
        // Attributes

        private static MainPage m_singleton = null;
        public static MainPage Singleton
        {
            get { return m_singleton; }
        }

        private int m_iChaseOffset = -1;


        /////////////////////////////////////////////////////////////
        // Construction

        HourglassCtrl hourglassCtrl1 = new HourglassCtrl();
        public MainPage()
        {

            // Initialize member variables
            m_singleton = this;

            // Initialize resources
            InitializeComponent();

            this.hourglassCtrl1.Margin = new Thickness(10, 10, 0, 0);
            this.hourglassCtrl1.Visibility = Visibility.Collapsed;

            // Setup library
            new LibrarySession(this);

            // Start loading data
            _loadSupportingData();

        }


        //<!--<myChaseWorkspace:ChaseAccountMenuCtrl x:Name="chaseAccountMenuCtrl"
        //        Width="300" MaxHeight="400" MinHeight="100"
        //        Grid.Row="2" Grid.RowSpan="2" Grid.Column="3"
        //        HorizontalAlignment="Right" VerticalAlignment="Top"
        //        Margin="0,0,14,0"
        //        Visibility="Collapsed" />-->

        /////////////////////////////////////////////////////////////
        // Implementaion of IDebtChasedMainPlugInWindow

        public void ShowHourglass(bool bTrue)
        {

            // Show/hide hourglass
            this.hourglassCtrl.IncShowCount(bTrue);

        }


        public void WebsiteMenuClicked(DebtChasedWebsiteMenuCtrl.eMenuItem menuItemId)
        {
            if (menuItemId != DebtChasedWebsiteMenuCtrl.eMenuItem.ChaseScreen_DebtDetails)
                ChaseScreen.DebtDetailViewState = DebtDetailViewState.Collapsed;

            // Compare menu items
            switch (menuItemId)
            {
                case DebtChasedWebsiteMenuCtrl.eMenuItem.ChaseScreen_Contacts:
                case DebtChasedWebsiteMenuCtrl.eMenuItem.ChaseScreen_DebtDetails:
                case DebtChasedWebsiteMenuCtrl.eMenuItem.ChaseScreen_Communications:
                case DebtChasedWebsiteMenuCtrl.eMenuItem.ChaseScreen_Tools:
                case DebtChasedWebsiteMenuCtrl.eMenuItem.ChaseScreen_Comments:
                case DebtChasedWebsiteMenuCtrl.eMenuItem.ChaseScreen_Agent:
                case DebtChasedWebsiteMenuCtrl.eMenuItem.Admin_Invoicing:
                    ChaseScreen.OnMenuItemClicked(menuItemId);
                    break;

                default:
                    break;
            } // switch

        }

        public void ContactPointButtonClick(PointActiveButton button, int iContactId, Guid contactPointId)
        {

            // Show call-results window
            ChaseScreen.ShowCallResultsWnd();

        }


        /////////////////////////////////////////////////////////////
        #region Operations loading and saving data

        private void _loadSupportingData()
        {
#if DEBUG
            Debug.Assert(CachedData.Singleton.CountryMap == null);
#endif // #if DEBUG

            // Show hourglass
            hourglassCtrl.IncShowCount(true);

            // Load real or fake supporting data
#if USEFAKEDATA

			// Create event
			object[] results = new object[1];
			results[0] = CachedData.GetDummyChaseWorkspaceSupportData();
			GetChaseWorkspaceSupportDataCompletedEventArgs e = new GetChaseWorkspaceSupportDataCompletedEventArgs(
				results,
				null,
				false,
				null
			);

			// Call completed event
			proxy_GetChaseWorkspaceSupportDataCompleted(null, e);

#else

            // Start by downloading the supporting data
            RichContentServiceClient proxy = new RichContentServiceClient();
            proxy.GetChaseWorkspaceSupportDataCompleted += new EventHandler<GetChaseWorkspaceSupportDataCompletedEventArgs>(proxy_GetChaseWorkspaceSupportDataCompleted);
            proxy.Endpoint.Address = ExtensionsForSilverlight.CreateDebtChasedEndpoint();
            proxy.GetChaseWorkspaceSupportDataAsync();

#endif // #if USEFAKEDATA

        }

        void proxy_GetChaseWorkspaceSupportDataCompleted(object sender, GetChaseWorkspaceSupportDataCompletedEventArgs e)
        {

            // Show hourglass
            hourglassCtrl.IncShowCount(false);

            // Get the result
            if (e.Error != null)
                MessageBoxes.PopupMessage(e.Error.Message);
            else if (e.Result == null)
                MessageBoxes.PopupMessage_InternalError("Failed to load supporting data from server.");
            else
            {

                // Update the cached data
                CachedData.Singleton.AddIncoming(e.Result);

                ChaseScreen.PostMainLoad();
                // Setup controls
            }

        }

        #endregion

        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ChaseScreen.OnMouseLeftButtonDown();
        }
    }


}
